<template>
  <div class="waterMeterLeftTree">
    <div class="title">
      <!-- <span>选择设备</span> -->
      <button class="titleButton" @click="clickLeft">
        <i class="el-icon-caret-left"></i>
      </button>
    </div>
    <div class="searchs">
      <el-input placeholder="请输入监测点名称" v-model="filterText" class="searchFont" clearable @change="filterChange" suffix-icon="el-icon-search">
      </el-input>
      <!-- <i class="el-icon-d-caret"></i> -->
      <!-- <button class="searchButton" @click="changeDefault">
            <i class="el-icon-d-caret"></i>
        </button> -->
      <!-- <i class="el-icon-caret-top"></i> -->
    </div>
    <!-- {{areaData}} -->
    <div class="tree-container filter-tree" style="overflow: auto; margin-right: 10px">
      <el-tree id="fixtree" class="tree el-scrollbar" ref="elTree" :data="treeData" :props="defaultProps" :default-expand-all="defaultTree" :lighlight-current="true" :current-node-key="currentNodeData" style="font-size: 12px" node-key="id" :filter-node-method="filterNode" show-checkbox :style="{ height: scrollerHeight, overflow: 'auto', minWidth: '100%' }" @check-change="nodeCheck">
        <!-- @node-click="handleNodeClick" -->
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span> <i :class="data.icon"></i>&nbsp;{{ node.label }} </span>
        </span>
        <!-- icon-class='el-icon-folder' -->
      </el-tree>
    </div>
  </div>
</template>

<script>
import {
  getTree, //区域树--小区资料全部
} from "@/api/baseManage/flScheduleCommunity/index";
import {
  searchflHydrantInfo, //查询区域组织下面的消防栓
} from "@/api/schedule/hydrant/flHydrantAlertEvents/index";
export default {
  data () {
    return {
      treeData: [],
      areaData: [],
      // areaChildrenData: [], // 存放子节点数据
      //   展开开关
      defaultTree: true,
      filterText: "",
      defaultProps: {
        children: "children",
        label: "label",
      },
      clickNode: "", // 存放点击节点
      getTreeList: [], //存放父树
      childNodeData: {}, // 存放子节点数据
      oRhydrantData: [],
      hydrantIds: [], //消防栓ids
      currentNodeData: null,
      defaultid: [],
    };
  },

  async created () {
    await this.getTreeData();
    // this.getsonNode();
  },
  mounted () { },
  computed: {
    scrollerHeight: function () {
      return window.innerHeight - 250 + "px";
    },
  },
  watch: {
    clickNode: {
      handler: function (val, olVal) {
        this.getsonNode(val);
      },
      // immediate: true,
      deep: true,
    },
    oRhydrantData: {
      handler (val) {
        console.log(val);
        //递归替换
        this.changeTreeData(this.areaData, val);
        this.changeLabel(this.treeData);
        this.$nextTick(() => {
          this.currentNodeData = this.treeData[0].children[0].children[0].id
          console.log(this.$refs.elTree);
          this.$nextTick(() => {
            this.$refs.elTree.setCheckedKeys([this.currentNodeData]); //获取已经设置的资源后渲染
          });
          this.hydrantIds = this.currentNodeData;
          // this.$refs.elTree.setCheckedNodes(this.treeData);
          // var allNodes = this.$refs.elTree.store._getAllNodes();
          // var leafNodes = allNodes.filter((item) => item.isLeaf);
          // let ids = [];
          // leafNodes.map((item) => {
          //   ids.push(item.data.id);
          // });
          // this.hydrantIds = ids.join(",")
          // this.hydrantIds = ids;
          // console.log(this.hydrantIds);
          // this.$emit("handleIds", this.hydrantIds);
        });
      },
    },
  },
  methods: {
    nodeCheck (nodeData, node, nodeComponent) {
      if (node) {
        let ids = [];
        this.currentNodeData = this.hydrantIds = nodeData.id;
        this.$refs.elTree.setCheckedNodes([nodeData]);
        console.log(this.currentNodeData, "单选的id");
        console.log(this.hydrantIds, "[][][]");
        this.handleNodeClick(nodeData, node, nodeComponent);
      }
    },
    changeTreeData (areaData, val) {
      let comparData = val;
      this.treeData = areaData.map((item) => {
        if (item.children) {
          this.changeTreeData(item.children, comparData);
        } else {
          comparData.map((i) => {
            if (item.code == i.code) {
              item = i;
            }
          });
        }
        return item;
      });
    },
    filterNode: function (value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    filterChange: function () {
      // 这里每当输入的数据有变化就触发原生的过滤节点这个函数
      this.$refs.elTree.filter(this.filterText);
    },

    handleNodeClick (nodeData, node, nodeComponent) {
      console.log(nodeData);
      this.clickNode = nodeData.sysCompanyCode;
      this.childNodeData = nodeData;
      this.$emit("getSearch", this.childNodeData, this.defaultid);
    },

    // 点击按钮
    clickLeft () {
      this.$emit("clickClose", "点击隐藏");
    },
    deepfor (data) {
      let length = data.length;
      for (let i = 0; i < length; i++) {
        if (data[i].hasOwnProperty("children")) {
          this.deepfor(data[i].children);
        } else {
          this.defaultid.push(data[i].id);
        }
      }
    },

    // 获取树资料
    async getTreeData (newVal) {
      let data;
      await getTree().then((res) => {
        console.log(res, "res");
        data = res.data;
      });
      // console.log(data,"我是data")
      this.getTreeList = data;
      this.areaData = this.getTreeList;
      this.changeLabel(this.areaData);
      this.getsonNode(this.areaData);
      // console.log(this.treeData,"this.treedata")
      // this.$nextTick(() => {    //这个如果要默认全选就必须加，否则会报错“setCheckedNodes”未定义
      //   this.$refs.elTree.setCheckedNodes(this.defaultid)
      // })
      // this.$emit("getSearch", this.areaData[0].children[0]);
    },
    // 获取站点数据
    getsonNode (val) {
      val.map((item) => {
        if (item.children) {
          this.getsonNode(item.children);
        } else {
          // item["children"] = [{ id: 1, label: "一" }]
          // debugger;
          let obj = {
            organization: item.code,
          };
          searchflHydrantInfo(obj)
            .then((reponse) => {
              item["children"] = reponse.data;
              this.oRhydrantData.push(item);
              this.deepfor(this.oRhydrantData);
            })
            .catch((error) => { });
        }
      });
    },
    // 转为label
    changeLabel (arr) {
      arr.forEach((item) => {
        for (var key in item) {
          if (key === "name") {
            item["label"] = item[key];
            // delete item[key]
          }
          if (key === "children" && item[key].length) {
            var arr1 = item[key];
            this.changeLabel(arr1);
          } else {
          }
        }
      });
    },
  },
  computed: {
    scrollerHeight: function () {
      return window.innerHeight - 250 + "px";
    },
  },
};
</script>

<style lang="scss" scoped>
.waterMeterLeftTree {
  /deep/.el-tree-node__content {
    // padding: 0 !important;
    height: 32px;
  }
  border-right: 1px solid #e6e6e6;
  border-bottom: 1px solid #e6e6e6;
  padding: 2px;
  // height: 85vh;
  // height: 100vh;
  max-width: 100%;
  // max-height: 200px;
  overflow:scroll .filter-tree {
    font-size: 12px !important;
    margin-right: 10px !important;
  }
  /deep/ .el-tree .el-tree-node .is-leaf + .el-checkbox .el-checkbox__inner {
    display: inline-block;
  }
  /deep/ .el-tree .el-tree-node .el-checkbox .el-checkbox__inner {
    display: none;
  }
  #fixtree {
    display: inline-block;
  }

  .tree /deep/ .el-tree-node {
    position: relative;
    padding-left: 15px;
  }

  .tree /deep/ .el-tree-node__children {
    padding-left: 12px;
  }

  .tree /deep/ .el-tree-node :last-child:before {
    height: 38px;
  }

  .tree /deep/ .el-tree > .el-tree-node:before {
    border-left: none;
  }

  .tree-container /deep/ .el-tree > .el-tree-node:after {
    border-top: none;
  }

  .tree /deep/ .el-tree-node:before {
    content: "";
    left: -4px;
    position: absolute;
    right: auto;
    border-width: 1px;
  }

  .tree /deep/ .el-tree-node:after {
    content: "";
    left: -4px;
    position: absolute;
    right: auto;
    border-width: 1px;
  }

  // .tree /deep/ .el-tree-node:before {
  //   border-left: 1px dashed #b8b9bb;
  //   bottom: 0px;
  //   height: 100%;
  //   top: -26px;
  //   width: 1px;
  // }

  // .tree /deep/ .el-tree-node:after {
  //   border-top: 1px dashed #b8b9bb;
  //   height: 20px;
  //   top: 12px;
  //   width: 24px;
  // }

  // ---------------------------------
  //有子节点 且未展开
  // .el-tree /deep/ .el-icon-caret-right:before {
  //   background: url("./image/xinzeng1.png") no-repeat 0 0;
  //   content: "";
  //   display: block;
  //   width: 10px;
  //   height: 10px;
  //   font-size: 10px;
  //   background-size: 10px;
  // }
  //有子节点 且已展开
  // .el-tree
  //   /deep/
  //   .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
  //   background: url("./image/jian.png") no-repeat 0 0;
  //   content: "";
  //   display: block;
  //   width: 10px;
  //   height: 10px;
  //   font-size: 10px;
  //   background-size: 10px;
  // }
  // 没有子节点
  // .el-tree /deep/.el-tree-node__expand-icon.is-leaf::before {
  //   background: url("./image/jian.png") no-repeat 0 0;
  //   content: "";
  //   display: block;
  //   width: 10px;
  //   height: 10px;
  //   font-size: 10px;
  //   background-size: 10px;
  // }
  .title {
    display: flex;
    justify-content: space-between;
    // border-bottom: 1px solid #e6e6e6;
    // padding: 10px 0 10px 10px;
    // height: 70px;
    .titleButton {
      width: 20px;
      height: 20px;
      line-height: 20px;
      border: 0;
      padding: 0;
      // margin-right: -2px;
      position: relative;
      left: 100.8%;
      .el-icon-caret-left {
        margin: -10px 0px 0px 0px;
        font-size: 16px;
      }
    }
  }
  .searchs {
    display: flex;
    // justify-content: center;
    // align-items: center;
    // top: 65px;
    // left: 25px;
    .searchFont {
      margin: 0px 5px 10px 5px;
      padding: 5px 2px;
      /deep/.el-input__inner {
        // width: 166px;
        height: 30px;
        line-height: 30px;
        padding: 5px 2px;
        width: 100%;
        // line-height: 30px;
        // padding: 5px 5px;
      }
      // .el-input__suffix {
      //     .el-input__suffix-inner {
      //     border-color: none;
      //     .el-icon-circle-close:before {
      //         content: "\e79d" !important;
      //         font-size: 18px;
      //     }
      //     }
      // }
    }
    .searchButton {
      border: 0;
      padding: 0;
      float: right;
      .el-icon-d-caret {
        margin: 0;
      }
    }
    .el-input__inner {
      margin-top: 15px;
    }
    .el-icon-search {
      position: relative;
      top: 10px;
      right: 5px;
      cursor: pointer;
    }
  }
}
</style>
